import { View } from "../../../../mvc/core/View";
import { PageTool } from "../../../../common/global/PageTool";
import { ItemData } from "../../../resource/info/ItemData";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { Cache } from "../../../cache/Cache";
import { MsgManager } from "../../../manager/MsgManager";
import { Language } from "../../../../../extend/language/Language";
import { ItemsUtil } from "../../shortcuts/item/ItemsUtil";
import { GameConst } from "../../../../component/gconst/GameConst";
import { SelectNumWindow } from "../../common/SelectNumWindow";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { ClockManager } from "../../../manager/ClockManager";
import { EGuildPosition } from "../../../../../Message/Game/EGuildPosition";
import { GuildAnimalType } from "../info/GuildAnimalType";
import { SGuildBeastInfo } from "../../../../../Message/Game/SGuildBeastInfo";
import { BossConfig } from "../../../resource/BossConfig";
import { GuildUtil } from "../info/GuildUtil";
import { LoaderHelp } from "../../../../common/display/LoaderHelp";
import { ResFileConst } from "../../../resource/ResFileConst";
import { GImageBitmap } from "../../../../../com/mui/controls/GImageBitmap";
import { ImagesConst } from "../../../resource/ImagesConst";
import { ObjCreate } from "../../common/ObjCreate";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { GScrollPane } from "../../../../../com/mui/controls/GScrollPane";
import { ScrollPolicy } from "../../../../../fl/controls/ScrollPolicy";
import { GuildAnimalRewardPanel2 } from "./GuildAnimalRewardPanel2";
import { GuildAnimalGrowthBar } from "./GuildAnimalGrowthBar";
import { GuildAnimalModelPanel } from "./GuildAnimalModelPanel";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { _type } from "../../../../../this/_type";
type int = number;
//class GuildAnimalPanel
    
    export  class GuildAnimalPanel extends View
    {
        constructor()
        {
            super();
            this._pageTool = new PageTool([], 5);
            this.init();
            return;
        }

        /* internal  */onBtnClickHandler(arg1: Object /* flash.events.Event */): void
        {
            var e: Object /* flash.events.Event */;
            var name: string;
            var day: int;
            var position: int;
            var onBtnClickHandler: Function;
            var items: Array<any>;
            var itemData: ItemData;
            var maxNum: int;

            var loc1;
            day = 0;
            position = 0;
            items = null;
            itemData = null;
            maxNum = 0;
            e = arg1;
            name = (e.target as GButton).name;
            if (name != "get") 
            {
                if (name != "call") 
                {
                    if (name == "feed") 
                    {
                        if (!Cache.instance.guildAnimal.isGeted) 
                        {
                            MsgManager.showRollTipsMsg(Language.getString(57526));
                            return;
                        }
                        items = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isGuildAnimalFood);
                        maxNum = (GameConst.GuildBeastGrowthThree - this._growth) / 10;
                        maxNum = 99;
                        if (items.length == 0) 
                        {
                            MsgManager.showRollTipsMsg(Language.getString(57542));
                            return;
                        }
                        itemData = items[0] as ItemData;
                        if (itemData.itemAmount > 1) 
                        {
                            if (this._selectNumWindow == null) 
                            {
                                this._selectNumWindow = new SelectNumWindow();
                            }
                            this._selectNumWindow.updateByItemData(itemData, this.onSelectNumOkHandler);
                            maxNum = maxNum >= itemData.itemAmount ? itemData.itemAmount : maxNum;
                            this._selectNumWindow.maxAmount = maxNum;
                            this._selectNumWindow.show();
                            return;
                        }
                        Dispatcher.dispatchEvent(new DataEvent(EventName.GuildAnimalFeed, {"uid": itemData.uid, "amount": itemData.itemAmount}));
                    }
                }
                else if (this._isCalled) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildAnimalGotoNpc));
                }
                else 
                {
                    onBtnClickHandler = (arg1: int): void
                    {
                        if (arg1 == Alert.OK) 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildAnimalCall));
                            Alert.resetToDefault();
                        }
                        return;
                    }
                    if (Cache.instance.guildAnimal.isCalled) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(57546));
                        return;
                    }
                    if (!this._isGeted) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(57526));
                        return;
                    }
                    day = ClockManager.instance.nowDate.getDay();
                    if (!(day == 2) && !(day == 4) && !(day == 6)) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(57527));
                    }
                    position = Cache.instance.guild.myGuildInfo.position;
                    if (!(position == EGuildPosition._EGuildLeader) && !(position == EGuildPosition._EGuildDeputyLeader)) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(57528));
                        return;
                    }
                    if (this._type < GuildAnimalType.LianQi) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(57529));
                        return;
                    }
                    Alert.show(Language.getStringByParam(57530, this._titleName, this._animalName, this._callCost), null, Alert.OK | Alert.CANCEL, null, onBtnClickHandler);
                }
            }
            else 
            {
                if (this._isGeted) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(57522));
                    return;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.GuildAnimalGetWindowOpen));
            }
            return;
        }

        public updateBySGuildBeastInfo(arg1: SGuildBeastInfo): void
        {
            var loc1=null;
            var loc2=0;
            if (arg1) 
            {
                loc1 = BossConfig.instance.getInfoByCode(arg1.bossId);
                this._modelPanel.updateModelByCode(arg1.bossId);
                this._isGeted = arg1.bossId > 0;
                this._isCalled = arg1.summoned;
                if (this._isCalled) 
                {
                    this._callBtn.label = Language.getString(21111);
                }
                else 
                {
                    this._callBtn.label = Language.getString(57512);
                }
                this._getBtn.enabled = !this._isGeted;
                this._growth = arg1.growth;
                this._type = GuildAnimalType.getTypeByGrowth(this._growth);
                this._titleName = GuildAnimalType.getTypeName(this._type);
                if (loc1) 
                {
                    this._animalName = loc1.boss.name;
                    this._nameTxt.text = this._animalName;
                    this._dotTxt.visible = true;
                }
                else 
                {
                    this._nameTxt.text = Language.getString(57548);
                }
                this._callCost = GuildAnimalType.getGrowthByType(this._type);
                this._growthBar.growth = this._growth;
                this._growthBar.toolTipData = this.getGrowthBarTipData();
                loc2 = this._type < 0 ? 0 : _type;
                this._rewardPanel.updateItemByIndex(int(GuildUtil.getTypeByBossId(arg1.bossId) + loc2));
                LoaderHelp.addResCallBack(ResFileConst.guildAnimal, this.updateTileBitmap);
            }
            return;
        }

        /* internal  */init(): void
        {
            this.addChild(new GImageBitmap(ImagesConst.GuildAnimalBg0));
            ObjCreate.createBg(360, 0, 303, 260, this);
            ObjCreate.createFlowerBg(0, 262, 665, 135, this);
            this._titleBitmap = new Object /* flash.display.Bitmap */();
            this._titleBitmap.x = 110;
            this._titleBitmap.y = 26;
            this.addChild(this._titleBitmap);
            this._dotTxt = ObjCreate.createTextField("•", 190, 25, 20, 20, this, new GTextFormat(FontUtil.songtiName, 16, 11661311, true));
            this._dotTxt.visible = false;
            this._nameTxt = ObjCreate.createTextField("", 205, 25, 220, 20, this, new GTextFormat(FontUtil.songtiName, 16, 15788607, true));
            this._nameTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._getBtn = ObjCreate.createGButton(Language.getString(57511), 95, 225, 70, 28, this);
            this._getBtn.name = "get";
            this._getBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClickHandler);
            this._callBtn = ObjCreate.createGButton(Language.getString(57512), 215, 225, 70, 28, this);
            this._callBtn.name = "call";
            this._callBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClickHandler);
            this._feedBtn = ObjCreate.createGButton(Language.getString(57513), 585, 350, 70, 28, this, "ButtonRedMini");
            this._feedBtn.name = "feed";
            this._feedBtn.toolTipData = Language.getString(57514);
            this._feedBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClickHandler);
            this._descText = ObjCreate.createTextField("", 0, 0, 270, 250, this);
            this._descText.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._descText.wordWrap = true;
            this._descText.multiline = true;
            this._descText.htmlText = Language.getString(57540);
            this._contentPane = new GScrollPane();
            this._contentPane.styleName = "GScrollPane";
            this._contentPane.horizontalScrollPolicy = ScrollPolicy.OFF;
            this._contentPane.verticalScrollPolicy = ScrollPolicy.ON;
            this._contentPane.width = 290;
            this._contentPane.height = 255;
            this._contentPane.source = this._descText;
            this._contentPane.move(370, 3);
            this.addChild(this._contentPane);
            this._rewardPanel = new GuildAnimalRewardPanel2();
            this._rewardPanel.x = 45;
            this._rewardPanel.y = 265;
            this.addChild(this._rewardPanel);
            this._growthBar = new GuildAnimalGrowthBar();
            this._growthBar.x = 70;
            this._growthBar.y = 355;
            this.addChild(this._growthBar);
            this._modelPanel = new GuildAnimalModelPanel();
            this._modelPanel.x = 180;
            this._modelPanel.y = 160;
            this.addChild(this._modelPanel);
            this._rewardPanel.updateItemByIndex(10);
            return;
        }

        /* internal  */getGrowthBarTipData(): string
        {
            var loc1=null;
            loc1 = Language.getStringByParam(57523, this._growth, this._titleName, GameConst.GuildBeastGrowthOne, GameConst.GuildBeastGrowthTwo, GameConst.GuildBeastGrowthThree);
            return loc1;
        }

        /* internal  */updateTileBitmap(): void
        {
            var loc1=GuildAnimalType.getBitmapNameByType(this._type);
            if (loc1) 
            {
                this._titleBitmap.bitmapData = GlobalClass.getBitmapData(loc1);
                this._nameTxt.x = 205;
            }
            else 
            {
                this._titleBitmap.bitmapData = null;
                if (Cache.instance.guildAnimal.isGeted) 
                {
                    this._nameTxt.x = 155;
                }
                else 
                {
                    this._nameTxt.x = 135;
                }
                this._dotTxt.visible = false;
            }
            return;
        }

        /* internal  */onSelectNumOkHandler(arg1: ItemData, arg2: int): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildAnimalFeed, {"uid": arg1.uid, "amount": arg2}));
            return;
        }

        /* internal */ static /* const */glowFilter: Object /* flash.filters.GlowFilter */=new Object /* flash.filters.GlowFilter */(15788607, 1, 4, 4, 10);

        /* internal */ /* var */_titleBitmap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_dotTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_nameTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_modelPanel: GuildAnimalModelPanel;

        /* internal */ /* var */_contentPane: GScrollPane;

        /* internal */ /* var */_descText: Object /* flash.text.TextField */;

        /* internal */ /* var */_rewardPanel: GuildAnimalRewardPanel2;

        /* internal */ /* var */_bitmap0: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_bitmap1: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_callCost: int;

        /* internal */ /* var */_titleName: string;

        /* internal */ /* var */_animalName: string;

        /* internal */ /* var */_type: int=-1;

        /* internal */ /* var */_growth: int;

        /* internal */ /* var */_isCalled: Boolean;

        /* internal */ /* var */_itemsArray: Array<any>;

        /* internal */ /* var */_pageTool: PageTool;

        /* internal */ /* var */_page: int=1;

        /* internal */ /* var */_feedBtn: GButton;

        /* internal */ /* var */_selectNumWindow: SelectNumWindow;

        /* internal */ /* var */_growthBar: GuildAnimalGrowthBar;

        /* internal */ /* var */_callBtn: GButton;

        /* internal */ /* var */_getBtn: GButton;

        /* internal */ /* var */_bitmap2: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_isGeted: Boolean;
    }
